package com.wdb.exception.handler;

import com.wdb.exception.BadRequestException;
import com.wdb.exception.EntityExistException;
import com.wdb.utils.ThrowableUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import java.nio.file.AccessDeniedException;

/**
 * 全局异常处理
 *
 * @author: wendaobai@qq.com
 * @date: 2024/1/9 0009 17:14
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

  /**
   * 不可知异常
   *
   * @param e
   * @return
   */
  @ExceptionHandler(Throwable.class)
  public ResponseEntity<ApiError> handleExcept(Throwable e) {
    log.error(ThrowableUtil.getStackTrance(e));
    return buildResponseEntity(ApiError.error(e.getMessage()));
  }

  @ExceptionHandler(AccessDeniedException.class)
  public ResponseEntity<ApiError> handleAccessDeniedException(
      AccessDeniedException e, HttpServletRequest request) {

    log.error(ThrowableUtil.getStackTrance(e));
    return buildResponseEntity(ApiError.error(HttpStatus.FORBIDDEN.value(), e.getMessage()));
  }

  // @ExceptionHandler(BadCredentialsException.class)
  public ResponseEntity<ApiError> handleBadCredentialsException(BadRequestException e) {
    log.info("BadCredentialsException 111");

    String message = "坏的凭证111";
    // String message = "坏的凭证".equals(e.getMessage()) ? "用户名或密码不正确" : e.getMessage();
    log.error(message);
    return buildResponseEntity(ApiError.error(message));
  }
  /** 处理 EntityExist */
  @ExceptionHandler(value = EntityExistException.class)
  public ResponseEntity<ApiError> entityExistException(EntityExistException e) {
    // 打印堆栈信息
    log.error(ThrowableUtil.getStackTrace(e));
    return buildResponseEntity(ApiError.error(e.getMessage()));
  }
  /** 自定义异常 */
  @ExceptionHandler(BadRequestException.class)
  public ResponseEntity<ApiError> badRequestException(BadRequestException e) {
    // 打印日志
    log.error(e.getMessage());
    return buildResponseEntity(ApiError.error(e.getStatus(), e.getMessage()));
  }
  /*
   *处理接口验证错误
   * */
  @ExceptionHandler(MethodArgumentNotValidException.class)
  public ResponseEntity<ApiError> MethodArgumentNotValidExceptionHandle(
      MethodArgumentNotValidException e) {
    log.info("MethodArgumentNotValidException 1111 ");
    // 打印日志
    // 打印堆栈信息
    log.error(ThrowableUtil.getStackTrace(e));
    ObjectError objectError = e.getBindingResult().getAllErrors().get(0);
    String message = objectError.getDefaultMessage();
    if (objectError instanceof FieldError) {
      message = ((FieldError) objectError).getField() + ": " + message;
    }
    return buildResponseEntity(ApiError.error(message));
  }
  /**
   * 统一返回接口数据
   *
   * @param apiError
   * @return
   */
  private ResponseEntity<ApiError> buildResponseEntity(ApiError apiError) {
    return new ResponseEntity<ApiError>(apiError, HttpStatus.valueOf(apiError.getStatus()));
  }
}
